package com.tcs.teg.web.accounts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/*
 * A better approach to extend from HandlerInterceptorAdapter  
 * instead of implementing HandlerInterceptor (where you would need to implement
 * preHandle, postHandle, afterCompletion methods. 
 * HandlerInterceptorAdapter  
 * Changed the comment
 * Version sourcing in GIT
 */

public class PerformanceInterceptor extends HandlerInterceptorAdapter {
	
	private static final Log logger = LogFactory.getLog(PerformanceInterceptor.class);
	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		long startTime = System.currentTimeMillis();
		request.setAttribute("startTime", startTime);
		logger.debug("adding adding adding adding");		
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		long startTime = (Long)request.getAttribute("startTime");
		request.removeAttribute("startTime");
		
		long endTime = System.currentTimeMillis();
		logger.debug("adding adding adding adding");
		modelAndView.addObject("handlingTime",  endTime-startTime);
		}
}
